package com.kzd.sdk.ui.fragment;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnAccessListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.utils.AppUtils;

public class KZD_AccessLoginFragment extends OwnerFragment {

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_access_login";
    }


    private LinearLayout llAccessLoginMobile, llAccessLoginAccount;
    private RadioGroup rgAccessLoginTitle;
    private RadioButton rbAccessLoginMobile, rbAccessLoginAccount;
    private TextView tvAccessLoginCodeGet, tvAccessLoginRegister, tvAccessLoginForget, tvAccessLoginSubmit, tvAccessLoginQuick;
    private EditText etAccessLoginMobile, etAccessLoginAccount, etAccessLoginCode, etAccessLoginPass;


    private String mSessionId = "";

    private final CountDownTimer mCountDownTimer = new CountDownTimer(1000 * 60, 1000) {
        public void onTick(long millisUntilFinished) {
            String text = "剩余" + millisUntilFinished / 1000 + "秒";
            tvAccessLoginCodeGet.setText(text);
        }

        public void onFinish() {
            tvAccessLoginCodeGet.setTextColor(Color.parseColor("#2983FF"));
            tvAccessLoginCodeGet.setEnabled(true);
            tvAccessLoginCodeGet.setText("获取验证码");
        }
    };

    private final KZD_HttpCallback<String> httpListenerPhoneCode = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            mSessionId = data;
            mCountDownTimer.start();
            tvAccessLoginCodeGet.setTextColor(Color.parseColor("#999999"));
            tvAccessLoginCodeGet.setEnabled(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    public KZD_OnAccessListener mOnAccessListener;

    public void setOnAccessListener(KZD_OnAccessListener onAccessListener) {
        this.mOnAccessListener = onAccessListener;
    }

    @Override
    public void initView(View view) {

        rgAccessLoginTitle = view.findViewById(AppUtils.getId(mContext, "rgAccessLoginTitle"));
        rbAccessLoginMobile = view.findViewById(AppUtils.getId(mContext, "rbAccessLoginMobile"));
        rbAccessLoginAccount = view.findViewById(AppUtils.getId(mContext, "rbAccessLoginAccount"));

        llAccessLoginMobile = view.findViewById(AppUtils.getId(mContext, "llAccessLoginMobile"));
        llAccessLoginAccount = view.findViewById(AppUtils.getId(mContext, "llAccessLoginAccount"));

        tvAccessLoginCodeGet = view.findViewById(AppUtils.getId(mContext, "tvAccessLoginCodeGet"));
        tvAccessLoginRegister = view.findViewById(AppUtils.getId(mContext, "tvAccessLoginRegister"));
        tvAccessLoginForget = view.findViewById(AppUtils.getId(mContext, "tvAccessLoginForget"));
        tvAccessLoginSubmit = view.findViewById(AppUtils.getId(mContext, "tvAccessLoginSubmit"));
        tvAccessLoginQuick = view.findViewById(AppUtils.getId(mContext, "tvAccessLoginQuick"));

        etAccessLoginMobile = view.findViewById(AppUtils.getId(mContext, "etAccessLoginMobile"));
        etAccessLoginAccount = view.findViewById(AppUtils.getId(mContext, "etAccessLoginAccount"));
        etAccessLoginCode = view.findViewById(AppUtils.getId(mContext, "etAccessLoginCode"));
        etAccessLoginPass = view.findViewById(AppUtils.getId(mContext, "etAccessLoginPass"));


        //标题选中事件
        rgAccessLoginTitle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switchMobileAndAccount(checkedId);
            }
        });

        //获取验证码点击事件
        tvAccessLoginCodeGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etAccessLoginMobile.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                } else {
                    KZD_ApiManager.httpPhoneCode(phone, 3, httpListenerPhoneCode);
                }
            }
        });

        //进入游戏点击事件
        tvAccessLoginSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitMobileAndAccount();
            }
        });

        //一键登录
        tvAccessLoginQuick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });

        //立即注册点击事件
        tvAccessLoginRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });

        //忘记密码点击事件
        tvAccessLoginForget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });
    }

    /**
     * 切换手机登录/密码登录
     */
    private void switchMobileAndAccount(int checkedId) {
        if (checkedId == AppUtils.getId(mContext, "rbAccessLoginMobile")) {
            rbAccessLoginMobile.setTextColor(Color.BLACK);
            rbAccessLoginMobile.setTextSize(18f);

            rbAccessLoginMobile.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            rbAccessLoginAccount.setTextColor(Color.parseColor("#FF999999"));
            rbAccessLoginAccount.setTextSize(12f);
            rbAccessLoginAccount.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);

            llAccessLoginAccount.setVisibility(View.GONE);
            llAccessLoginMobile.setVisibility(View.VISIBLE);
            tvAccessLoginForget.setVisibility(View.GONE);
        } else {
            rbAccessLoginMobile.setTextColor(Color.parseColor("#FF999999"));
            rbAccessLoginMobile.setTextSize(12f);
            rbAccessLoginMobile.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);

            rbAccessLoginAccount.setTextColor(Color.BLACK);
            rbAccessLoginAccount.setTextSize(18f);
            rbAccessLoginAccount.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);

            llAccessLoginMobile.setVisibility(View.GONE);
            llAccessLoginAccount.setVisibility(View.VISIBLE);
            tvAccessLoginForget.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 登录
     */
    private void submitMobileAndAccount() {
        if (rbAccessLoginMobile.isChecked() && TextUtils.isEmpty(etAccessLoginMobile.getText())) {
            Toast.makeText(mContext, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (rbAccessLoginMobile.isChecked() && TextUtils.isEmpty(etAccessLoginCode.getText())) {
            Toast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (rbAccessLoginAccount.isChecked() && TextUtils.isEmpty(etAccessLoginAccount.getText())) {
            Toast.makeText(mContext, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (rbAccessLoginAccount.isChecked() && TextUtils.isEmpty(etAccessLoginPass.getText())) {
            Toast.makeText(mContext, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        //手机验证码登录
        String userMobile = etAccessLoginMobile.getText().toString();
        String userCode = etAccessLoginCode.getText().toString();
        if (rbAccessLoginMobile.isChecked() && mOnAccessListener != null && mSessionId != null) {
            mCountDownTimer.cancel();
            mOnAccessListener.onLoginPhone(userMobile, userCode, mSessionId);
        }

        //账号密码登录
        String username = etAccessLoginAccount.getText().toString();
        String password = etAccessLoginPass.getText().toString();
        if (rbAccessLoginAccount.isChecked() && mOnAccessListener != null) {
            mOnAccessListener.onLoginAccount(username, password);
        }
    }
}
